#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       useradd.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-06-23 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import os
import mysqlDB
import random
import XiaoTool as xt
import time

def main(site="health-product-es.com"):
	if os.path.exists("%s.ini" % site):
		print("File already exists!")
		exit()

	f = open("name.txt")
	fixlist = [l.strip() for l in f.readlines()]
	f.close()
	
	#fixlist = random.sample(flist, 50)
	userddName = []
	db = mysqlDB.Mydb(host='localhost', dataBase='wp', user='wp', passwd='wp')
	db.dbLoad()
	
	inUserSql = "INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES (NULL, '%s', '3290629c64d545e3c7f423f7450a2504', '%s', '%s', '', '%s', '', 0, '%s')"

	inUsermetaSql = """INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES 
					(NULL, {uid}, 'first_name', ''),
					(NULL, {uid}, 'last_name', ''),
					(NULL, {uid}, 'nickname', '%s'),
					(NULL, {uid}, 'description', ''),
					(NULL, {uid}, 'rich_editing', 'true'),
					(NULL, {uid}, 'comment_shortcuts', 'false'),
					(NULL, {uid}, 'admin_color', 'fresh'),
					(NULL, {uid}, 'use_ssl', '0'),
					(NULL, {uid}, 'show_admin_bar_front', 'true'),
					(NULL, {uid}, 'show_admin_bar_admin', 'false'),
					(NULL, {uid}, 'aim', ''),
					(NULL, {uid}, 'yim', ''),
					(NULL, {uid}, 'jabber', ''),
					(NULL, {uid}, 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'),
					(NULL, {uid}, 'wp_user_level', '10');"""
	
	for sName in fixlist:
		uName = xt.url_clear(sName)
		userSql = inUserSql % (uName, uName, "%s@%s.com" % (uName, sName.split()[0]), time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()), uName)
		db.save(userSql)
		usermetaSql = inUsermetaSql.replace("{uid}", str(db.last_id()))
		db.save(usermetaSql % uName)

		userddName.append(uName)

	db.dbClose
	fw = open("%s.ini" % site, "w")
	fw.write("\n".join(userddName))
	fw.close()

if __name__ == "__main__":
	main()
	print("ok!")
